package TouristManagement.ResponsableSite;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.TreeMap;

import TouristManagement.*;

public class ResponsableSite {
//	
//	private short idSite;
//	private Site leSite;
	private ArrayList<Site> lesSites = new ArrayList<Site>();
	private ServerNamingTourist leServerNamingTourist;
	private TouristOffice leTouristOffice;
		
	public void initServer(){
		this.connectToServerNamingTourist();
		this.connectToTouristOfficeServer();
		this.connectToSiteServers();
	}
	
	/*
	 * connecter au server de nommage de l'application
	 */
	public void connectToServerNamingTourist(){
		//init VisitManagement server
		try {
			
	        String args[]=null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent("ServerNamingTourist","");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantServerNamingTourist = nameRoot.resolve(nameToFind);
	        
	        leServerNamingTourist = ServerNamingTouristHelper.narrow(distantServerNamingTourist);
	        
	        System.out.println("Tourist naming server connected.");
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/*
	 * connecter au server de l'office tourisme
	 */
	public void connectToTouristOfficeServer(){
		try {
			
			Properties props = new Properties();
	        props.put("org.omg.CORBA.ORBInitialPort", "2001");
	        props.put("org.omg.CORBA.ORBInitialHost", "127.0.0.1");
	        
	        String args[] = null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));

	        // Construction du nom a rechercher
	        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
	         nameToFind[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.nomTouristOffice(),"");

	        // Recherche aupres du naming service
	        org.omg.CORBA.Object distantTouristOffice = nameRoot.resolve(nameToFind);
	        System.out.println("TouristOffice server connected.");
//	        System.out.println(orb.object_to_string(distantTouristOffice));

	        leTouristOffice = TouristOfficeHelper.narrow(distantTouristOffice);	      
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/*
	 * connecter au server de toutes les sites
	 */	
	public void connectToSiteServers(){
		try {			
			
			short[] lesIdSites = leTouristOffice.getIdSite();
			
	        String args[] = null;

			// Intialisation de l'orb
			org.omg.CORBA.ORB orb = org.omg.CORBA.ORB.init(args,null);

	        // Recuperation du naming service
	        org.omg.CosNaming.NamingContext nameRoot =
	        		org.omg.CosNaming.NamingContextHelper.narrow(orb.resolve_initial_references("NameService"));
	        
	        //connect to sites
	        for (short i:lesIdSites){
		        // Construction du nom a rechercher
		        org.omg.CosNaming.NameComponent[] nameToFind = new org.omg.CosNaming.NameComponent[1];
		         nameToFind[0] = new org.omg.CosNaming.NameComponent(leServerNamingTourist.nomSites()[i-1],"");
	
		        // Recherche aupres du naming service
		        org.omg.CORBA.Object distantSite = nameRoot.resolve(nameToFind);
		        System.out.println("Site "+ leServerNamingTourist.nomSites()[i-1] + " connected, id "+i);
		        
		        lesSites.add(SiteHelper.narrow(distantSite));	 
	        }	        
		}
		catch (Exception e) {
			e.printStackTrace();
		}		
	}
	
	/*
	 * get la liste des sites et leurs noms
	 */
	public Map<Short,String> getListSite(){
		Map<Short,String> result = new TreeMap<Short,String>();
		for (Site s:lesSites) {
			System.out.println(s.idSite()+". "+s.getNomSite());
			result.put(s.idSite(), s.getNomSite());			
		}	
		return result;
	}
	
	/*
	 * consulter les statistiques locales d'un site
	 */
	public Map<String,String> consultStatLocal(short idSite){
		Site leSite = null;
		for (Site s:lesSites) {
			if (idSite==s.idSite()) {
				leSite = s;
			}					
		}
		Map<String,String> localStatMap = new HashMap<String,String>();
		LocalStat localStat = leSite.consultLocalStat();
		localStatMap.put("nameSite", leSite.getNomSite());
		localStatMap.put("affluenceQuotidienne", String.valueOf(localStat.affluenceQuotidienne));
		localStatMap.put("durationAverage", String.valueOf(localStat.durationAverage));
		localStatMap.put("durationMax", String.valueOf(localStat.durationMax));
		localStatMap.put("durationMin", String.valueOf(localStat.durationMin));		
		return localStatMap;
	}	
}
